﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using YArchitech.Controls;
using YJKArchUtils.Utils;
using YJKRevitFloorModule.FloorOffset;

namespace YJKRevitFloorModule.Forms
{
	public partial class FloorOffsetForm : System.Windows.Forms.Form
	{
		public FloorOffsetForm()
		{
			this.InitializeComponent();
		}

		public double Offset
		{
			get
			{
				double result;
				try
				{
					result = Common.MMToFeet(Convert.ToDouble(this.tbOffset.Text));
				}
				catch (Exception)
				{
					result = this.m_Offset;
				}
				return result;
			}
			set
			{
				this.tbOffset.Text = Common.FeetToMM(value).ToString("f0");
			}
		}

		private void FloorOffsetForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.SetFocusToRevit();
			this.CancelRevitOperate();
		}

		private void FloorOffsetForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		private void FloorOffsetForm_Load(object sender, EventArgs e)
		{
			this.ReadData();
			this.tbOffset.Text = "0";
		}

		private void FloorOffsetForm_MouseLeave(object sender, EventArgs e)
		{
			if (!Common.IsValidating(this.tbOffset.Text, -500.0, 500.0))
			{
				YJKMessageBox.Show("合法范围在-500mm～500mm之间");
				this.m_Offset = 0.0;
				this.tbOffset.Text = this.m_Offset.ToString("f0");
			}
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				this.SetFocusToRevit();
				return;
			}
			base.Focus();
		}

		private void ReadData()
		{
			string path = Path.GetTempPath() + "FloorOffsetForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			try
			{
				this.m_Offset = Convert.ToDouble(streamReader.ReadLine());
			}
			catch (Exception)
			{
				streamReader.Close();
				fileStream.Close();
			}
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "FloorOffsetForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.WriteLine(this.m_Offset);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SetFocusToRevit()
		{
			int wMsg = 7;
			FloorOffsetForm.SendMessage(CmdFloorOffset._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void CancelRevitOperate()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			FloorOffsetForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			FloorOffsetForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void tbOffset_TextChanged(object sender, EventArgs e)
		{
			if (Common.IsValidating(this.tbOffset.Text, -500.0, 500.0))
			{
				this.m_Offset = Convert.ToDouble(this.tbOffset.Text);
			}
		}

		private double m_Offset;
	}
}
